<template>
  <div>
    <!-- 头部 -->
    <div class="dy-header" @scroll="handleScroll">
      <div class="dy-fix" v-show="top<30">
        <van-icon name="arrow-left" @click="back" />
        <h3>课程详情</h3>
        <img src="../../assets/img/share.png" alt />
      </div>

      <div class="dy-abo" v-show="top>30">
        <van-icon name="arrow-left" @click="back" />
        <div>
          <p
            @click="scale(index)"
            v-for="(item,index) in dy_header"
            :key="index"
            :class="{dy_avtive:dy_index==index}"
          >{{item}}</p>
        </div>
        <img src="../../assets/img/share.png" alt />
      </div>
    </div>

    <!-- 课程 -->
    <div class="dy-wen">
      <p>{{dy_List.info.classify_title}}</p>
      <p>免费</p>
      <p>
        <span>共1课时</span> |
        <span>{{dy_List.info.sales_num}}人已报名</span>
      </p>
      <div class="dy-van" @click="sc">
        <van-icon
          name="star"
          class="dy-van"
          :class="{'ic-active':dy_List.info.is_collect}"
          color="red"
          v-show="dy_flag"
          @click="sc(dy_List.info.collect_id)"
        />
      </div>
    </div>

    <!-- 教学团队 -->
    <div class="dy-team">
      <p>教学团队</p>
      <p v-for="(i,a) in dy_List.teachers" :key="a">
        <img :src="i.teacher_avatar" alt />
      </p>
      <p v-for="(i,a) in dy_List.teachers" :key="a">{{i.teacher_name}}</p>
    </div>

    <!-- 课程介绍 -->
    <div class="dy-jie">
      <p>课程介绍</p>
      <p v-html="dy_List.info.course_details"></p>
    </div>

    <!-- 课程大纲 -->
    <div class="dy-gang">
      <p>课程大纲</p>
      <div class="dy-cir" v-for="(item,index) in dy_gang" :key="index">
        <div class="p"></div>
        <p>{{item.periods_title}}</p>
      </div>
    </div>

    <!-- 课程评论 -->
    <div class="dy-view">
      <p>课程评论</p>
      <img src="../../assets/img/view.png" alt v-if="dy_view.length==0" />
      <ul class="dy-list" v-else>
        <li v-for="(item,index) in dy_view" :key="index">
          <img :src="item.avatar" alt />
          <div class="dy-wenzi">
            <p>
              {{item.nickname}}
              <span>
                <van-rate
                  v-model="value"
                  :size="12"
                  color="rgb(234, 122, 47)"
                  void-icon="star"
                  void-color="#eee"
                />
              </span>
            </p>
            <p>{{item.content}}</p>
          </div>
          <p>{{item.created_at | fomartTime('MM月dd日 hh:mm')}}</p>
        </li>
      </ul>
    </div>

    <!-- 立即报名 -->
    <button class="dy-btn" @click="submit">{{state==0?'立即报名':'立即学习'}}</button>
  </div>
</template>

<script>
import { corse, Gang, View, collect, nocollect } from "../../request/http";
// import {formatDate} from '../../plugin/formate';
export default {
  name: "",
  data() {
    return {
      dy_List: [],
      dy_gang: [],
      dy_view: [],
      value: 5,
      dy_header: ["课程介绍", "课程大纲", "课程评价"],
      dy_index: 0,
      dy_show: true,
      time: 1516703495241,
      top: 0,
      dy_flag: true,
      state: 0
    };
  },
  filters: {
    // formatDate(time) {
    //       var date = new Date(time);
    //       return formatDate(date, 'yyyy-MM-dd hh:mm');
    //   }
  },
  props: [],
  components: {},
  mounted() {
    this.getList();
    Gang(this.$route.query.id).then(res => {
      // console.log(res);
      this.dy_gang = res;
    });
    View(this.$route.query.id).then(res => {
      console.log(res.list);
      this.dy_view = res.list;
    });
  },
  methods: {
    getList() {
      corse(this.$route.query.id).then(res => {
        console.log(res);
        if (res.info.is_buy || res.info.is_join_study) {
          this.state = 1;
        }
        console.log(this.state);
        this.dy_List = res;
      });
    },
    back() {
      this.$router.go(-1);
    },
    scale(index) {
      this.dy_index = index;
    },
    handleScroll() {
      this.top = document.body.scrollTop || document.documentElement.scrollTop;
    },
    //报名
    submit() {
      console.log(
        this.dy_List.info.id,
        this.dy_List.info.course_type,
        this.dy_List
      );
      if (this.dy_List.info.is_free) {
        this.$http
          .post(`/order/downOrder`, {
            shop_id: this.dy_List.info.id,
            type: this.dy_List.info.course_type
          })
          .then(res => {
            console.log(res);

            this.$toast({
              message: "成功",
              type: "success",
              duration: 1000
            });
            this.getdetail();
          });
      } else {
        if (
          this.dy_List.info.stock == 0 &&
          this.dy_List.info.store_num > 0 &&
          (this.dy_List.info.course_type == 7 || this.dy_List.info.course_type == 3)
        ) {
          this.$toast({
            message: "你来晚了哦,名额已经没有了~",
            type: "success",
            duration: 1500
          });
          return;
        }
      }
    },
    getdetail() {
      this.$http
        .get(`/courseInfo/basis_id=${this.$route.query.id}`)
        .then(res => {
          console.log(res);
          if (res.info.is_buy || res.info.is_join_study) {
            this.state = 1;
          }
          console.log(this.state);
          this.dy_List = res;
        });
    },
    sc() {
      let obj = {
        course_basis_id: this.dy_List.info.id,
        type: 1
      };
      if (!this.dy_List.info.is_collect) {
        collect(obj).then(() => {
          this.$toast({
            message: "收藏成功",
            type: "success",
            duration: 800
          });
        });
      } else {
        nocollect(this.dy_List.info.collect_id).then(() => {
          this.$toast({
            message: "取消收藏",
            durationL800
          });
        });
      }
      this.getList();
    }
  },
  activated() {
    window.addEventListener("scroll", this.handleScroll);
  },
  deactivated() {
    //页面即将替换新的页面的时候
    window.removeEventListener("scroll", this.handleScroll);
  }
};
</script>

<style scoped>
.ic-active {
  color: red;
}
.dy-header {
  background: #fff;
  height: 50px;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: 100;
  width: 100%;
}
.dy-fix {
  width: 100%;
  height: 50px;
  background: #fff;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: 100;
}
.dy-fix {
  display: flex;
  align-items: center;
  padding: 0 10px;
}
.dy-fix > img {
  width: 20px;
  height: 20px;
}
.dy-fix > .van-icon {
  font-size: 20px;
}
.dy-fix > h3 {
  font-size: 18px;
  width: 300px;
  text-align: center;
  line-height: 50px;
}
.dy-wen {
  margin-top: 50px;
  position: relative;
}
.dy-wen {
  background: #fff;
  padding: 15px;
}
.dy-wen > p:nth-of-type(1) {
  font-size: 25px;
}
.dy-wen > p:nth-of-type(2) {
  font-size: 17px;
  color: #eb6100;
}
.dy-wen > p:nth-of-type(3) {
  font-size: 14px;
  margin-top: 10px;
  color: rgba(0, 0, 0, 0.45);
}
.dy-wen > .dy-van {
  position: absolute;
  font-size: 20px;
  color: #333;
  top: 15px;
  right: 15px;
}
.dy-team {
  width: 100%;
  background: #fff;
  margin-top: 17px;
  padding: 15px 15px 30px 15px;
  box-sizing: border-box;
}
.dy-team > p:nth-of-type(1) {
  font-size: 17px;
}
.dy-team > p:nth-of-type(2) > img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-top: 15px;
  margin-left: 15px;
}
.dy-team > p:nth-of-type(3) {
  margin-top: 5px;
  margin-left: 20px;
}
.dy-jie {
  background: #fff;
  padding: 15px;
  margin-top: 17px;
}
.dy-jie > p:nth-of-type(1) {
  font-size: 17px;
}
.dy-jie > p:nth-of-type(2) {
  font-size: 15px;
  margin-top: 10px;
}
.dy-gang {
  margin-top: 17px;
  background: #fff;
  padding: 15px;
}
.dy-gang > p:nth-of-type(1) {
  font-size: 17px;
}
.dy-gang > p:nth-of-type(2) {
  font-size: 17px;
  margin-top: 10px;
}
.dy-view {
  background: #fff;
  margin-top: 17px;
  padding: 15px;
  min-height: 200px;
  margin-bottom: 40px;
}
.dy-view > p:nth-of-type(1) {
  font-size: 17px;
}
.dy-view > img {
  width: 200px;
  height: 200px;
  margin-left: 60px;
}
.dy-btn {
  background: #eb6100;
  font-size: 0.45333rem;
  font-size: 4.53333vw;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  color: #fff;
  border: none;
  outline: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 40px;
}
.dy-cir {
  display: flex;
  align-items: center;
  margin-top: 10px;
}
.dy-cir > .p {
  width: 5px;
  height: 5px;
  background: #eb6100;
  border-radius: 50%;
}
.dy-cir > p {
  margin-left: 5px;
  font-size: 14px;
}
.dy-list > li {
  display: flex;
  padding: 10px 0;
}
.dy-list > li > img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
}
.dy-list > li > .dy-wenzi {
  margin-left: 10px;
  width: 300px;
}
.dy-list > li > .dy-wenzi > p:nth-of-type(2) {
  margin-top: 10px;
  color: #999999;
}
.dy-list > li > p {
  color: #999999;
}
.dy-abo {
  width: 100%;
  height: 50px;
  background: #fff;
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  padding: 0 10px;
}
.dy-abo > img {
  width: 20px;
  height: 20px;
}
.dy-abo > .van-icon {
  font-size: 20px;
}
.dy-abo > div {
  font-size: 14px;
  width: 300px;
  display: flex;
  /* text-align: center; */
  line-height: 50px;
}
.dy-abo > div > p {
  width: 100px;
  text-align: center;
  color: #8c8c8c;
}
.dy-abo > div > .dy_avtive {
  font-size: 17px;
  color: #000;
}
</style>